/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.query;

import java.net.*;
import javax.net.*;
import javax.net.ssl.*;

import nfse.*;
import nfse.result.*;

import java.util.*;

//import com.sleepycat.je.*;

public class QueryServer extends Thread {
	private int numQueries = 0;
	private final int MAX_QUERIES = 200;
	private int port = -1;
	private int queryID = 0;
	private Vector<Query> queries = new Vector<Query>();
	private ResultServer resultServer = null;

	public QueryServer(int portNum, ResultServer rs) {
		this.port = portNum;
		this.resultServer = rs;
	}

	public int getPort() {
		return port;
	}

	public synchronized void endQuery() {
		if (numQueries > 0)
			numQueries--;
	}

	public synchronized boolean startQuery() {
		if (numQueries == MAX_QUERIES) {
			System.out.println("MAX_QUERIES exceeded!!");
			return false;
		}
		numQueries++;
		return true;
	}

	public void run() {
		try {
			System.out.println("Query Server: Listening on port " + port + ".");
			ServerSocketFactory ssf = null;
			System.out.println();
			if (NetFSE.USE_SSL_QUERY)
				ssf = SSLServerSocketFactory.getDefault();
			else
				ssf = ServerSocketFactory.getDefault();
			ServerSocket ss = ssf.createServerSocket(port);
			while (true) {
				if (this.startQuery()) {
					new QuerySocket(ss.accept(), this).start();
					System.out
							.println("Query Server: Opened new query socket.");
				} else
					Thread.sleep(1000);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public synchronized int nextQueryID() {
		if (queryID == 5000000)
			queryID = 0;
		return ++queryID;
	}

	public ResultServer getResultServer() {
		return resultServer;
	}

	public void addQuery(Query q) throws QueryException {
		if (q == null) {
			throw (new QueryException("Tried to add null query."));
		}
		queries.addElement(q);
	}

	public Query getQuery(int queryID) {
		Query temp = null;
		for (int i = 0; i < queries.size(); i++) {
			Query q = (Query) queries.elementAt(i);
			if (q.getID() == queryID) {
				temp = q;
				break;
			}
		}
		return temp;
	}

	public void removeQuery(int queryID) {
		for (int i = 0; i < queries.size(); i++) {
			Query q = (Query) queries.elementAt(i);
			if (q.getID() == queryID) {
				queries.removeElementAt(i);
				break;
			}
		}
	}

}